import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Timer;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class MainFrame extends JFrame implements ActionListener{

	private static final long serialVersionUID = 1L;
	public Image ico;
	public JLabel lblTimeInfo,lblTime,lblShutInfo,lblLastTimeInfo,lblLastTime;
	public JComboBox cbxTimes;
	public JButton btnStart, btnCancel;
	
	public MainFrame(){
		
		ico = Toolkit.getDefaultToolkit().getImage("pic/clock.png");
		this.setIconImage(ico);
		this.setTitle("定时关机软件  by余正忠");
		this.setBounds(0, 0, 300, 160);
		this.setLocationRelativeTo(null);
		this.setResizable(false);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setLayout(null);
		this.setAlwaysOnTop(true); 
		
		initCom();
		
		this.repaint();
	}
	
	public static void main(String[] args){
		MainFrame frame = new MainFrame();
		frame.setVisible(true);
		
		Timer t1 = new Timer();
		t1.scheduleAtFixedRate(new ShowTimeThread(frame), 0, 1000);
	}
	
	public void actionPerformed(ActionEvent e){
		if(e.getSource() == btnStart){
			
			int lastTime = TimeUtil.transferCbxValue(cbxTimes.getSelectedItem().toString().trim());
			
			Timer t2 = new Timer();
			t2.scheduleAtFixedRate(new LastTimeThread(this, lastTime), 0, 1000);
			
			cbxTimes.setEnabled(false);
			this.getContentPane().remove(btnStart);
			this.getContentPane().add(btnCancel);
			this.repaint();
		}else if(e.getSource() == btnCancel){
			System.exit(0);
		}
	}
	
	private void initCom(){
		lblTimeInfo = new JLabel("现在时间：");
		lblTimeInfo.setBounds(5, 5, 100, 25);
		this.getContentPane().add(lblTimeInfo);
		
		lblTime = new JLabel("Loading time...");
		lblTime.setBounds(110, 5, 200, 25);
		this.getContentPane().add(lblTime);
		
		lblShutInfo = new JLabel("选择关机时间：");
		lblShutInfo.setBounds(5, 35, 100, 25);
		this.getContentPane().add(lblShutInfo);
		
		cbxTimes = new JComboBox();
		cbxTimes.addItem("1分钟后");
		cbxTimes.addItem("10 分后");
		cbxTimes.addItem("20 分后");
		cbxTimes.addItem("30 分后");
		cbxTimes.addItem("40 分后");
		cbxTimes.addItem("50 分后");
		cbxTimes.addItem("1小时后");
		cbxTimes.setBounds(110, 35, 140, 25);
		this.getContentPane().add(cbxTimes);
		
		lblLastTimeInfo = new JLabel("剩余时间：");
		lblLastTimeInfo.setBounds(5, 65, 100, 25);
		this.getContentPane().add(lblLastTimeInfo);
		
		lblLastTime = new JLabel("暂无");
		lblLastTime.setBounds(110, 65, 200, 25);
		this.getContentPane().add(lblLastTime);
		
		btnStart = new JButton("开始");
		btnStart.setBounds(115, 95, 80, 28);
		btnStart.setBorderPainted(false);
		btnStart.addActionListener(this);
		this.getContentPane().add(btnStart);
		
		btnCancel = new JButton("取消");
		btnCancel.setBorderPainted(false);
		btnCancel.setBounds(115, 95, 80, 28);
		btnCancel.addActionListener(this);
	}
}
